package com.glorysoft.hlgdpda.ui.materialandtool.viewmodel

import androidx.databinding.ObservableArrayList
import com.glorysoft.hlgdpda.base.BaseViewModel
import com.glorysoft.hlgdpda.base.Constant
import com.glorysoft.hlgdpda.entity.MLotListEntity
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.model.TaskRepository
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser

/**
 * FileName: MaterialViewModel
 * Author: XJH
 * Date: 2021/5/14 17:53
 * Description:
 * History:
 */
class MaterialViewModel : BaseViewModel() {

    val bankList : List<String> = listOf("No.","批号","规格描述","数量","选择")

    private val materialRepository = TaskRepository()
    var positionList = ObservableArrayList<String>()
    var materialLotList = ObservableArrayList<String>()
    var positionNameList = ObservableArrayList<String>()
    var eqpID : String = ""
    var materialLot : String = ""
    var positionName : String = ""
    var toolID : String = ""
    var WarehouseID : String = ""
    var lotID : String = ""

    fun initViewModel(){
        eqpID = ""
        materialLot = ""
        positionName = ""
        toolID = ""
        lotID = ""
        WarehouseID = ""
        positionList.clear()
        materialLotList.clear()
    }

    /**
     * 获取物料信息
     */
    fun searchMaterial() {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE","LOAD")
        map.addProperty("EQUIPMENTID",eqpID)
        launchOnlyResult({
            val mLotInfo = materialRepository.getMLotInfo(map)
            positionList.clear()
            materialLotList.clear()
            mLotInfo.MLOTLIST.forEach{
                if (!positionList.contains(it.POSITIONNAME)){
                    positionList.add(it.POSITIONNAME)
                }
                materialLotList.add(it.MLOT.MLOTID)
            }
        })
    }

    /**
     * 物料上下机
     */
    fun mLotKitting(actionType : String){
        if (eqpID.isEmpty() || materialLot.isEmpty()){
            defUI.toastEvent.value = ToastMessageBean("设备号或物料批次不能为空",false)
            return
        }
        val map = JsonObject()
        map.addProperty("ACTIONTYPE",actionType)
        map.addProperty("EQUIPMENTID",eqpID)
        map.addProperty("OPERATOR",Constant.CURRENT_USER)
        //todo:// 仓库
        map.addProperty("TOWAREHOUSEID","")
        map.addProperty("POSITIONNAME",positionName)
        val list: ArrayList<MLotListEntity.MLotEntity.MLotInfo> = arrayListOf()
        val entity = MLotListEntity.MLotEntity.MLotInfo()
        entity.MLOTID = materialLot
        list.add(entity)
        map.add("MLOTLIST", JsonParser().parse(Gson().toJson(list)).asJsonArray)
        launchOnlyResult({
            materialRepository.mLotKitting(map)
            defUI.callObserve.call()
            initViewModel()
        })
    }

    /**
     * Bank库上下料
     */
    fun bankShip(actionType : String){
        val map = JsonObject()
        map.addProperty("ACTIONTYPE",actionType)
        map.addProperty("LOTID",lotID)
        map.addProperty("OPERATOR",Constant.CURRENT_USER)
        map.addProperty("WAREHOUSEID",WarehouseID)
        //todo:// 位ID
        map.addProperty("STOAGEID","")
        launchOnlyResult({
            materialRepository.bankShip(map)
            defUI.callObserve.call()
            initViewModel()
        })
    }

    fun getPositionName(){
        val map = JsonObject()
        map.addProperty("EQUIPMENTID",eqpID)
        map.addProperty("TYPE","EQPPOSITIONSET")
        map.addProperty("ACTIONTYPE","QUERY")
        launchOnlyResult({
            val positionName = materialRepository.getPositionName(map)
            if (positionName.POSITIONSET.POSITIONSETLINES.isNotEmpty()) {
                positionName.POSITIONSET.POSITIONSETLINES.forEach{
                    positionNameList.add(it.POSITIONNAME)
                }
            }
        })

    }

    fun bindTool(){

    }
}